<template>
	<view id="qrcode">
		<view class="userList">
			<image  class="img" :src="qrImg" alt="" @click="setImg()" v-if='qrImg'/>
			<image  class="img" src="../../static/my/upload.png" alt="" @click="setImg()" v-else/>
		</view>
		<view class="padded" style="padding-top:2rem;">
			<view  class="sureBtn" @click='sure'>确认</view>
		</view>
		<view class="padded" style="padding-top:20rpx">
			<view  class="sureBtn" @click='clear' style="background:#FFA500">清除</view>
		</view>
		<view class="tip">提示: 上传微信二维码,系统将自动生成带该二维码的海报素材放在素材库里,方便你的业务</view>
	</view>
</template>

<script>
	
	export default {
		data() {
			return {
				qrImg:''
			}
		},
		onLoad(){
			this.httpClient.post('agents/app/agent/getUserQrCodeexp',{}).then(res=>{
				if(res.code==200){
					this.qrImg = res.data.expUrl
				}
			})
		},
		methods: {
			setImg(){
				let that = this;
				uni.chooseImage({
					count: 1,
					sizeType: 'compressed',
					success: (chooseImageRes) => {
						let imgPath = chooseImageRes.tempFilePaths[0]
						this.httpClient.upload('agents/app/agent/uploadQrcode',{
							name: 'file',
							filePath: imgPath
						}).then(res=>{
							if(res.code==200){
								that.qrImg = res.data.src
							}
						})
					}
				})
			},
			sure(){
				this.httpClient.post('agents/app/agent/updateUserQrCode',{
					qrCode:this.qrImg
				}).then(res=>{
					if(res.code==200){
						uni.showToast({
							icon: 'success',
							title: res.msg
						})
						setTimeout(function() {
							uni.navigateBack({
								delta:1
							})
						}, 1000)
					}
				})
			},
			clear(){
				let that=this
				this.httpClient.post('agents/app/agent/updateUserQrCode',{
					qrCode:''
				}).then(res=>{
					that.qrImg=''
					if(res.code==200){
						uni.showToast({
							icon: 'success',
							title: res.msg
						})
						setTimeout(function() {
							uni.navigateBack({
								delta:1
							})
						}, 1000)
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	#qrcode{
		background-color: #FFF;padding:50rpx 30rpx;
		.userList{
			text-align: center;
			.img{
				width: 300rpx;
				height:300rpx;
			}
		}		
		.tip{
			padding: 20rpx;
			color: #888;
			font-size: 24rpx;
		}
		.padded{
			padding:20rpx;
			.sureBtn{
			    background: #3aacff;
			    text-align: center;
			    color: #fff;
			    line-height: 2.8rem;
			    border-radius: 0.5rem;
			}
		}
	}
	
</style>
